package com.ruoyi.user.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.PermissionService;
import com.ruoyi.user.constant.QbUserConstant;
import com.ruoyi.user.domain.QbUser;
import com.ruoyi.user.domain.QbUserWx;
import com.ruoyi.user.mapper.QbUserMapper;
import com.ruoyi.user.mapper.QbUserWxMapper;
import com.ruoyi.user.service.IqbUserService;
import com.ruoyi.user.util.QbDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 人员信息Service业务层处理
 *
 * @author lzhj
 * @date 2021-01-17
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class QbUserServiceImpl implements IqbUserService {
    @Autowired
    private QbUserMapper qbUserMapper;

    @Autowired
    private QbUserWxMapper qbUserWxMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询人员信息
     *
     * @param id 人员信息ID
     * @return 人员信息
     */
    @Override
    public QbUser selectQbUserById(Long id) {
        return qbUserMapper.selectQbUserById(id);
    }

    /**
     * 查询人员信息列表
     *
     * @param qbUser 人员信息
     * @return 人员信息
     */
    @Override
    public List<QbUser> selectQbUserList(QbUser qbUser) {
        String userId = SecurityUtils.getUsername();
        if (permissionService.hasRole("admin")) {
            userId = null;
        }
        return qbUserMapper.selectQbUserList(qbUser, userId);
    }

    @Override
    public List<QbUser> selectQbUserShList(QbUser qbUser) {
        String userId = SecurityUtils.getUsername();
        if (permissionService.hasRole("admin")) {
            userId = null;
        }
        return qbUserMapper.selectQbUserShList(qbUser, userId);
    }

    /**
     * 新增人员信息
     *
     * @param qbUser 人员信息
     * @return 结果
     */
    @Override
    public int insertQbUser(QbUser qbUser) {
        qbUser.setCreateBy(SecurityUtils.getUsername());
        return qbUserMapper.insertQbUser(qbUser);
    }

    /**
     * 修改人员信息
     *
     * @param qbUser 人员信息
     * @return 结果
     */
    @Override
    public int updateQbUser(QbUser qbUser) {
        qbUser.setUpdateTime(DateUtils.getNowDate());
        QbUserWx qbUserWx = qbUserWxMapper.selectQbUserWxByUserId(qbUser.getId());
        if (qbUserWx != null) {
            qbUserWx.setCategory(qbUser.getCategory());
            qbUserWx.setZjh(qbUser.getZjh());
            qbUserWxMapper.updateQbUserWx(qbUserWx);
        }
        return qbUserMapper.updateQbUser(qbUser);
    }

    /**
     * 批量删除人员信息
     *
     * @param ids 需要删除的人员信息ID
     * @return 结果
     */
    @Override
    public int deleteQbUserByIds(Long[] ids) {
        int i = qbUserMapper.deleteQbUserByIds(ids);
//        qbUserWxMapper.deleteQbUserWxByUserid(ids);
        for (Long id : ids) {
            QbUserWx qbUserWx = qbUserWxMapper.selectQbUserWxByUserId(id);
            if (qbUserWx != null) {
                int n = qbUserWxMapper.deleteQbUserWxById(qbUserWx.getId());
                String openId = qbUserWx.getOpenId();
                List<QbUser> qbUserList = qbUserMapper.queryUserListByOpenid(openId);
                //设置微信绑定的用户的缓存
                redisCache.setCacheObject(QbDataUtil.getWxUserCacheKey(openId), qbUserList);
            }

        }
        return i;
    }

    /**
     * 删除人员信息信息
     *
     * @param id 人员信息ID
     * @return 结果
     */
    @Override
    public int deleteQbUserById(Long id) {
        int i = qbUserMapper.deleteQbUserById(id);
        QbUserWx qbUserWx = qbUserWxMapper.selectQbUserWxByUserId(id);
        if (qbUserWx != null) {
            int n = qbUserWxMapper.deleteQbUserWxById(qbUserWx.getId());
            String openId = qbUserWx.getOpenId();
            List<QbUser> qbUserList = qbUserMapper.queryUserListByOpenid(openId);
            //设置微信绑定的用户的缓存
            redisCache.setCacheObject(QbDataUtil.getWxUserCacheKey(openId), qbUserList);
        }
        return i;
    }

    @Override
    public Boolean checkRepeat(QbUser qbUser) {
        String sql = "select count(1) from t_qb_user where zjh=? and category=? and (id!=? or ? is null)";
        Integer i = jdbcTemplate.queryForObject(sql, Integer.class, qbUser.getZjh(), qbUser.getCategory(), qbUser.getId(), qbUser.getId());
        return i > 0 ? true : false;
    }

    @Override
    public void passUser(Long[] ids, Double price) {
        String username = SecurityUtils.getUsername();
        for (Long id : ids) {
            QbUser qbUser = qbUserMapper.selectQbUserById(id);
            if (qbUser != null) {
                qbUser.setUpdateBy(username);
                qbUser.setStatus(QbUserConstant.USER_SHTG);
                qbUser.setPrice(price);
                qbUserMapper.updateQbUser(qbUser);
            }

        }
    }

    @Override
    public QbUser getUserByZjhAndCategory(String zjh, String category, String status) {
        QbUser qbUser = qbUserMapper.getUserByZjhAndCategory(zjh, category, status);
        return qbUser;
    }


    @Override
    public QbUser getQbUserByWx(QbUser qbUser) {
        QbUser qbUser1 = qbUserMapper.getQbUserByWx(qbUser);
        return qbUser1;
    }

    @Override
    public List<QbUser> queryUserListByOpenid(String openId) {
        List<QbUser> qbUserList = qbUserMapper.queryUserListByOpenid(openId);
        return qbUserList;
    }


}
